//初始化城市
function initData(callback){
	var cityList = new Array();
	cityList.push({name:'香港',});
	cityList.push({name:'北京',});
	cityList.push({name:'上海',});
	cityList.push({name:'广州',});
	cityList.push({name:'杭州',});
	callback(cityList);
}

//加载城市数据
function loadCityData(city,callback){
	
	var date = new Date();
	var dateStr = date.format("MM月dd日  yyyy年");
	
	var url = 'http://www.google.com/ig/api?hl=zh-cn&weather='+city.name+'&ie=utf-8';
	
	
	
			
			
	//var url = 'api.xml';
	$.ajax({
		url:url,
		type: 'GET',
		dataType:'html', 
		success:function(text){
			var dom = $(text);

			var data = {
				current:{
					humidity:dom.find("current_conditions").find("humidity").attr("data").replace("湿度： ",""),
					condition:dom.find("current_conditions").find("condition").attr("data"),
					temp:dom.find("current_conditions").find("temp_c").attr("data"),
					lowTemp:$(dom.find("forecast_conditions")[0]).find("low").attr("data"),
					highTemp:$(dom.find("forecast_conditions")[0]).find("high").attr("data"),
					wind:dom.find("current_conditions").find("wind_condition").attr("data").replace("风向： ",""),
					image:dom.find("current_conditions").find("icon").attr("data"),
					dayOfWeek:$(dom.find("forecast_conditions")[0]).find("day_of_week").attr("data")
				}};
			var forecastList = new Array();
			dom.find("forecast_conditions").each(function(){
				var forecast = {};
				forecast.dayOfWeek = $(this).find("day_of_week").attr("data");
				forecast.lowTemp = $(this).find("low").attr("data");
				forecast.highTemp = $(this).find("high").attr("data");
				forecast.image = $(this).find("icon").attr("data");
				forecastList.push(forecast);
			});
			data.forecastList  = forecastList;
			
			$.log($.toJSON(data));
			callback(city,data);
		
		},
		error:function(){
			var data = new Object();
			callback(city,data);
		},
	})
}

/*
* 刷新城市天气
* box :对应的div
* city : 城市
* data : loadCityData 请求得到的数据
*/
function refreshWeather(box,city,data){
	
	var reg  = new RegExp("[^/\\\\]+$","g"); 
	var image =  reg.exec(data.current.image).toString();
	image = image.replace(".gif",".png");
		
		
	var date = new Date();
	var dateStr = date.format("MM月dd日  yyyy年");
	var con = '';
	con += '<div class="air-temperature">';
		con += '<img style="width:148px;height:148px;"  src="'+PAGE_PATH+'images/weather/'+image+'"/>';
		con += '<div class="city-temperature"><span class="city">'+city.name+'</span><span class="temperature">'+data.current.temp+'°C</span></div>';
		con += '<div class="date-weather"><span class="week-day">'+data.current.dayOfWeek+'</span><span class="date">'+dateStr+'</span><div class="weather">'+data.current.condition+'</div></div>';
	con += '</div>';
	
	con += '<ul class="other">';
		con += '<li class="odd"><span class="item-title">湿度:</span><span class="item-value">'+data.current.humidity+'</span></li>';
		con += '<li class="even"><span class="item-title">高温:</span><span class="item-value">'+data.current.highTemp+'°C</span></li>';
		con += '<li class="odd"><span class="item-title">能见度:</span><span class="item-value">2 km</span></li>';
		con += '<li class="even"><span class="item-title">低温:</span><span class="item-value">'+data.current.lowTemp+'°C</span></li>';
		con += '<li class="odd"><span class="item-title">风向:</span><span class="item-value">'+data.current.wind +'</span></li>';
	con += '</ul>';
	
	con += '<div class="next-four-days-weather">';
	
		
		for(var i=0;i<data.forecastList.length;i++)
		{
		var reg  = new RegExp("[^/\\\\]+$","g"); 
        var image =  reg.exec(data.forecastList[i].image).toString();
		image = image.replace(".gif",".png");
		con += '<span class="first"><div class="week-day">'+data.forecastList[i].dayOfWeek+'</div><div class="temperature">'+data.forecastList[i].lowTemp+'℃-'+data.forecastList[i].highTemp+'℃</div><img style="width:60px;height:65px;" src="'+PAGE_PATH+'images/weather/'+image+'"/></span>';
		}

	con += '</div>';	
	box.html(con);
}


							
						
                        
                        
							
							
							
							
							
						
                        
                        
							
							
							
							
						